/**
 * Created with IntelliJ IDEA.
 * User: zzl
 * Date: 13-1-3
 * To change this template use File | Settings | File Templates.
 */
(function ($, undefined) {

    function Clipboard() {

    }

    var proto = { constructor: Clipboard };
    Clipboard.prototype = proto;

    proto.setTable = function (table) {
        this.$table = $(table);
    };

    proto.copy = function (nodes) {
        var me = this;
        me.nodeAndDescendantsArr = [];
        for (var n = 0; n < nodes.length; n++) {
            var node = nodes[n];
            var nodeAndDescendants = [node];
            todolist.collectDescendants(node, nodeAndDescendants);
            var $clone = $(nodeAndDescendants).clone();
            $clone.removeClass('active selected');
            nodeAndDescendants = $clone.toArray();
            me.nodeAndDescendantsArr.push(nodeAndDescendants);
        }
    };

    proto.pasteByRootNodesHtml = function(html, relNode, below, asChild) {
        var me = this;
        me.nodeAndDescendantsArr = [];
        html = jsex.trim(html);
        var $nodes = $(html).filter('tr');
        $nodes.each(function(n ,it) {
            buildNodeDecorations(it);
        });

        var nodeAndDescendants;
        for(var n = 0; n < $nodes.length; n++) {
            var node = $nodes[n];
            $(node).find('.editable').attr('tabindex', '0');
            var level = $(node).data('level');
            if(level == 0) {
                nodeAndDescendants = [node];
                me.nodeAndDescendantsArr.push(nodeAndDescendants);
            }
            else {
                nodeAndDescendants.push(node);
            }
        }
        var pastedNodes = me.paste(relNode, below, asChild);
        return pastedNodes;
    };

    proto.paste = function (relNode, below, asChild) {
        var me = this;
        if (!me.nodeAndDescendantsArr || !me.nodeAndDescendantsArr.length) {
            return [];
        }
        todolist.clearSelect();
        if(!relNode) {
            relNode = me.$table.children('tbody.nodes').children('tr:first');
            asChild = true;
            below = true;
        }
        var $relNode = $(relNode);
        var relLevel = $relNode.data('level') || 0;
        var pastedNodes = [];
        var n, node;
        var nodeAndDescendants, node0, levelDiff, i;
        if (!asChild && !below) {
            for (n = 0; n < me.nodeAndDescendantsArr.length; n++) {
                nodeAndDescendants = $(me.nodeAndDescendantsArr[n]).clone();
                node0 = nodeAndDescendants[0];
                levelDiff = relLevel - $(node0).data('level');
                for (i = 0; i < nodeAndDescendants.length; i++) {
                    node = nodeAndDescendants[i];
                    $relNode.before(node);
                    pastedNodes.push(node);
                }
                todolist.setupNodeRelations(nodeAndDescendants);
                if (levelDiff) {
                    todolist.updateBranchLevel(node0, levelDiff);
                }
            }
        }
        else if (!asChild && below) {
            var relNodeAndDescendants = [$relNode[0]];
            todolist.collectDescendants($relNode, relNodeAndDescendants);
            var $tRelNode = $(relNodeAndDescendants[relNodeAndDescendants.length - 1]);
            for (n = me.nodeAndDescendantsArr.length - 1; n >= 0; --n) {
                nodeAndDescendants = $(me.nodeAndDescendantsArr[n]).clone();
                node0 = nodeAndDescendants[0];
                levelDiff = relLevel - $(node0).data('level');
                for (i = nodeAndDescendants.length - 1; i >= 0; --i) {
                    node = nodeAndDescendants[i];
                    $tRelNode.after(node);
                    pastedNodes.push(node);
                }
                todolist.setupNodeRelations(nodeAndDescendants);
                if (levelDiff) {
                    todolist.updateBranchLevel(node0, levelDiff);
                }
            }
            pastedNodes.reverse();
        }
        else if (asChild) {
            for (n = me.nodeAndDescendantsArr.length - 1; n >= 0; --n) {
                nodeAndDescendants = $(me.nodeAndDescendantsArr[n]).clone();
                node0 = nodeAndDescendants[0];
                levelDiff = relLevel + 1 - $(node0).data('level');
                var $afterNode;
                if(below) {
                    var lastChildNode = todolist.getLastChild($relNode);
                    $afterNode = lastChildNode? $(lastChildNode): $relNode;
                }
                else {
                    $afterNode = $relNode;
                }
                for (i = nodeAndDescendants.length - 1; i >= 0; --i) {
                    node = nodeAndDescendants[i];
                    $afterNode.after(node);
                    pastedNodes.push(node);
                }
                todolist.setupNodeRelations(nodeAndDescendants);
                if (levelDiff) {
                    todolist.updateBranchLevel(node0, levelDiff);
                }
            }
            pastedNodes.reverse();
        }
        $(node).trigger('nodeAdded', [pastedNodes]);
        var parentNode = asChild? $relNode[0]: todolist.getParentNode($relNode);
        todolist.updateChildrenAndExpand(parentNode, true);
        todolist.select(pastedNodes);

        todolist.updateIndentLinesExtended(pastedNodes);
        if(!asChild && below) {
            todolist.updateIndentLinesExtended(relNode);
        }

        //todolist.updateAltRows();
        for(var n = 0; n < pastedNodes.length; n++) {
            $(pastedNodes[n]).dataAttr('id', null);
        }
        return pastedNodes.slice();
    };


    window.clip = new Clipboard();
    window.Clipboard = Clipboard;

})(jQuery);